package wp.wf.com.myapplication.bean;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

/**
 * Created by KF on 2016/4/25.
 */
public class MyViewHolder extends RecyclerView.ViewHolder {
    private SparseArray<View> listView = new SparseArray<>();
    private View parentView;

    public MyViewHolder(View itemView) {
        super(itemView);
        parentView = itemView;
    }

    public <T extends View> T findViewById(int id) {
        View view = listView.get(id);
        if (view == null) {
            view = parentView.findViewById(id);
            listView.put(id, view);
        }
        return (T) view;
    }

    public static MyViewHolder getViewHolder(int layoutId, Context content) {
        View view = LayoutInflater.from(content).inflate(layoutId, null);
        return new MyViewHolder(view);
    }

    public void setText(int id, String info) {
        TextView textView = findViewById(id);
        textView.setText(info);
    }

}
